﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Models;

namespace ChinaDynastyTours.Views
{
    public partial class RestaurantEvaluateForm : DevExpress.XtraEditors.XtraForm
    {
        private DataTable _dtRestaurantEvaluateData;
        private string _TourgroupID { get; set; }

        public RestaurantEvaluateForm(string tourgroupId)
        {
            InitializeComponent();
            _TourgroupID = tourgroupId;
        }
        private void RestaurantEvaluateForm_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_TourgroupID))//如果没传进来团号
            {
                //初始化团下拉列表
                DataSet ds = TeamGroupCtrl.GetAllIDForTeamGroup();
                if (ds.Tables.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                        btnTeamGroupID.Properties.Items.Add(dr["TourGroupID"].ToString().Trim());
                }
            }
            else
            {
                btnTeamGroupID.Text = _TourgroupID;
                btnTeamGroupID.Enabled = false;
                restaurantGridView.OptionsBehavior.Editable = false;
                restaurantGridControl.DataSource = RestaurantEvaluateCtrl.GetRestaurantEvaluateData(_TourgroupID); 
            }
        }
        private void btnTeamGroupID_SelectedIndexChanged(object sender, EventArgs e)
        {
            _dtRestaurantEvaluateData = RestaurantEvaluateCtrl.GetRestaurantEvaluateData(btnTeamGroupID.Text);

            if (_dtRestaurantEvaluateData == null || _dtRestaurantEvaluateData.Rows.Count == 0)
            {
                string tourgroupName = TeamGroupCtrl.GetTeamGroupNameById(btnTeamGroupID.Text);

                
                DataTable tmpDt = RestaurantEvaluateCtrl.GetTravelOrderForRestaurantEval(btnTeamGroupID.Text).Tables[0];

                foreach (DataRow dr in tmpDt.Rows)
                {
                    RestaurantEvaluateEntity entity = new RestaurantEvaluateEntity()
                    {
                        TourgroupID = btnTeamGroupID.Text,
                        TourgroupName = tourgroupName,
                        ProviderID = int.Parse(dr["TravelAgencyID"].ToString()),
                        ProviderName = dr["TravelAgencyName"].ToString(),
                        CityName = dr["City"].ToString(),
                        Good = 0,
                        Middle = 0,
                        Common = 0,
                        Bad = 0,
                        Praise = 0,
                        Complain = 0,
                        Remark = "",
                    };
                    RestaurantEvaluateCtrl.CreateRestaurantEvaluateData(entity);
                }
                _dtRestaurantEvaluateData = RestaurantEvaluateCtrl.GetRestaurantEvaluateData(btnTeamGroupID.Text);
            }
            //加载数据到UI的gride
            restaurantGridControl.DataSource = _dtRestaurantEvaluateData;
            _dtRestaurantEvaluateData.RowChanged += new DataRowChangeEventHandler(_dtRestaurantEvaluateData_RowChanged);
        }

        void _dtRestaurantEvaluateData_RowChanged(object sender, DataRowChangeEventArgs e)
        {
            if (e.Action == DataRowAction.Change || e.Action == DataRowAction.ChangeCurrentAndOriginal || e.Action == DataRowAction.ChangeOriginal)
            {
                //string ProviderID = feedbackGridView.GetRowCellValue(feedbackGridView.FocusedRowHandle, "ProviderID").ToString();

                RestaurantEvaluateEntity entity = new RestaurantEvaluateEntity()
                {
                    EvaluateID = int.Parse(restaurantGridView.GetRowCellValue(restaurantGridView.FocusedRowHandle, "EvaluateID").ToString()),
                    TourgroupID = e.Row["TourgroupID"].ToString(),
                    TourgroupName = e.Row["TourgroupName"].ToString(),
                    CityName = e.Row["CityName"].ToString(),
                    ProviderName = e.Row["ProviderName"].ToString(),
                    ProviderID = int.Parse(e.Row["ProviderID"].ToString()),
                    Good = int.Parse(e.Row["Good"].ToString()),
                    Middle = int.Parse(e.Row["Middle"].ToString()),
                    Common = int.Parse(e.Row["Common"].ToString()),
                    Bad = int.Parse(e.Row["Bad"].ToString()),
                    Praise = int.Parse(e.Row["Praise"].ToString()),
                    Complain = int.Parse(e.Row["Complain"].ToString()),
                    Remark = e.Row["Remark"].ToString()
                };
                RestaurantEvaluateCtrl.UpdateRestaurantEvaluateData(entity);
            }
        }
    }
}